//
//  PicProgressView.swift
//  BellManage
//
//  Created by Bestsch on 16/6/6.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class PicProgressView: UIView {
    
    var imageView: UIImageView!
    var progressView: DALabeledCircularProgressView!
    
    var imageUrl: String! {
        
        didSet {
            imageView.sd_setImage(with: URL.init(string: imageUrl), placeholderImage: nil, options: SDWebImageOptions.retryFailed, progress: { (receivedSize, expectedSize) in
                
                let progress: CGFloat = 1.0 * CGFloat(receivedSize) / CGFloat(expectedSize)
               
                self.progressView.progress = progress
                self.progressView.isHidden = false
                self.progressView.progressLabel.text = String(format: "%.0f%%",progress * 100)
                
            }) { (image, error, cachType, imageUrl) in
                
                self.progressView.isHidden = true
            }
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        imageView.frame = self.bounds
        progressView.frame = CGRect(x: (self.bounds.size.width - 50)/2, y: (self.bounds.size.height - 50)/2, width: 50, height: 50)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUp()
    }
    func setUp() {
        
        
        imageView = UIImageView.init(frame: CGRect.zero)
        imageView.contentMode = UIViewContentMode.scaleAspectFill
        imageView.clipsToBounds = true
        imageView.backgroundColor = UIColor.init(hexString: "dbdbdb", alpha: 1)
        
        progressView = DALabeledCircularProgressView.init(frame: CGRect.zero)
        progressView.roundedCorners = 5
        progressView.progressLabel.textColor = UIColor.white
        progressView.progressLabel.font = UIFont.systemFont(ofSize: 13)
        
        self.addSubview(imageView)
        self.addSubview(progressView)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
